package com.atkxp.web; /**
 * @declaration
 * @author user
 * @create 2022/9/25-18:23
 */

import com.atkxp.domain.Book;
import com.atkxp.domain.Page;
import com.atkxp.service.BookService;
import com.atkxp.service.impl.BookServiceImpl;
import com.atkxp.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "BookServlet", value = "/manager/bookServlet")
public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNum;
        int pageSize;
        if(request.getParameter("pageNum") == null){
            pageNum = 1;
        }else{
            pageNum = Integer.parseInt(request.getParameter("pageNum"));
        }

        if(request.getParameter("pageSize") == null){
            pageSize = Page.PAGE_SIZE;
        }else{
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }

        Page<Book> page = bookService.page(pageNum, pageSize);
        page.setUrl("manager/bookServlet?action=page");

        request.setAttribute("page", page);

        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Book book = WebUtils.copyParameterToBean(request.getParameterMap(), new Book());
        bookService.addBook(book);
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNum=" + request.getParameter("pageNum"));
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        int id1 = Integer.parseInt(id);
        bookService.deleteBookById(id1);
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNum=" + request.getParameter("pageNum"));
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Book> books = bookService.queryBooks();
        request.setAttribute("books", books);
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Book book = WebUtils.copyParameterToBean(request.getParameterMap(), new Book());
        bookService.updateBook(book);
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNum=" + request.getParameter("pageNum"));
    }

    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String id = request.getParameter("id");
            Book book = bookService.queryBookById(Integer.parseInt(id));
            request.setAttribute("book", book);
            request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request, response);
    }
}
